
//
//  GPMiddleView.m
//  GPFM
//
//  Created by 郭鹏 on 2018/3/2.
//  Copyright © 2018年 郭鹏. All rights reserved.
//

#import "GPMiddleView.h"
#import "CALayer+PauseAimate.h"
@interface GPMiddleView()

@property (weak, nonatomic) IBOutlet UIButton *playBtn;
@property (weak, nonatomic) IBOutlet UIImageView *middleImageView;

@end

@implementation GPMiddleView

#pragma mark - 初始化

- (void)awakeFromNib
{
    [super awakeFromNib];
    
    [self setAnimation];
    
    [self setMiddleView];
    
    [self setAction];
    
}
- (void)setAnimation
{
    [self.middleImageView.layer removeAnimationForKey:@"playAnnimation"];
    
    CABasicAnimation *basicAnnimation = [[CABasicAnimation alloc] init];
    basicAnnimation.keyPath = @"transform.rotation.z";
    basicAnnimation.fromValue = @0;
    basicAnnimation.toValue = @(M_PI * 2);
    basicAnnimation.duration = 30;
    basicAnnimation.repeatCount = MAXFLOAT;
    basicAnnimation.removedOnCompletion = NO;
    
    [self.middleImageView.layer addAnimation:basicAnnimation forKey:@"playAnnimation"];
    [self.middleImageView.layer pauseAnimate];
}
- (void)setMiddleView
{
    self.middleImageView.layer.masksToBounds = YES;
    self.midImage = self.middleImageView.image;
}
- (void)setAction
{
    [self.playBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    // 监听播放状态的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(isPlayerPlay:) name:@"playState" object:nil];

    // 监听播放图片的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setPlayImage:) name:@"playImage" object:nil];
}
- (void)setIsPlaying:(BOOL)isPlaying
{
    if (_isPlaying == isPlaying) {
        return;
    }
    _isPlaying = isPlaying;
    if (isPlaying) {
        [self.playBtn setImage:nil forState:UIControlStateNormal];
        [self.middleImageView.layer resumeAnimate];
        
    }else {
        
        UIImage *image = [UIImage imageNamed:@"tabbar_np_play"];
        [self.playBtn setImage:image forState:UIControlStateNormal];
        
        [self.middleImageView.layer pauseAnimate];
    }
}
#pragma mark - 内部方法
- (void)isPlayerPlay:(NSNotification *)notification {
    BOOL isPlay = [notification.object boolValue];
    self.isPlaying = isPlay;
}
- (void)setPlayImage:(NSNotification *)notification {
    UIImage *image = notification.object;
    self.midImage = image;
}
- (void)btnClick:(UIButton *)btn {
    
    if (self.middleClickBlock) {
        self.middleClickBlock(self.isPlaying);
    }
}
#pragma mark - 单例
static GPMiddleView *_shareInstance;
+ (instancetype)shareInstance
{
    if (_shareInstance == nil) {
        _shareInstance = [GPMiddleView middleView];
    }
    return _shareInstance;
}
+ (instancetype)middleView
{
    NSBundle *currenBundle = [NSBundle bundleForClass:self];

    GPMiddleView *middleView = [currenBundle loadNibNamed:NSStringFromClass([GPMiddleView class]) owner:nil options:nil
                                ].firstObject;
    return middleView;
}
@end
